﻿Imports System.Net.NetworkInformation

Public Class Device
    Inherits Button

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    Private components As System.ComponentModel.IContainer

    Sub New(ByVal device_id As String)
        Me.Device_id = device_id
        Me.components = New System.ComponentModel.Container()
        Me.Timer_int_change = New System.Windows.Forms.ToolStripMenuItem()
        Me.Device_close = New System.Windows.Forms.ToolStripMenuItem()
        Me.timer = New Timer
        Me.btn_contextmenu = New ContextMenuStrip(Me.components)
        Me.ping = New Ping

        btn_contextmenu.AutoSize = True
        '
        'ButtonCtnxMenu
        '
        Me.btn_contextmenu.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.Timer_int_change, Me.Device_close})
        Me.btn_contextmenu.Name = "ButtonCtnxMenu"
        Me.btn_contextmenu.Size = New System.Drawing.Size(182, 48)
        '
        'Timer_int_change
        '
        Me.Timer_int_change.Name = "Timer_int_change"
        Me.Timer_int_change.Size = New System.Drawing.Size(181, 22)
        Me.Timer_int_change.Text = "Change Timer Interval"
        '
        'Close
        '
        Me.Device_close.Name = "Close"
        Me.Device_close.Size = New System.Drawing.Size(181, 22)
        Me.Device_close.Text = "Close"
        '
        'Button
        '
        'Me.Location = New System.Drawing.Point(3, 3)
        Me.ContextMenuStrip = Me.btn_contextmenu
        Me.Name = Me.Device_id
        Me.Size = New System.Drawing.Size(180, 100)
        Me.TabIndex = 0
        Me.Text = Me.Device_id
        Me.UseVisualStyleBackColor = True
        '
        'timer
        '
        timer.Interval = 5000
        timer.Enabled = True
        timer.Start()
    End Sub
    Public Sub ChangeTimerDuration(ByVal span As Integer)
        timer.Stop()
        timer.Interval = span
        timer.Start()
    End Sub
    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick
        'Display Ping message in textbox
        With ping.Send(Device_id)
            If .Status = IPStatus.Success Then
                Me.BackColor = Color.Green
                Me.Text = Me.Device_id & " (" & .RoundtripTime.ToString & ")"
            Else
                Me.BackColor = Color.Red
                Me.Text = Me.Device_id & " (" & .Status & ")"
            End If
        End With
    End Sub

#Region "Context Menu"
    Private Sub Device_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Device_close.Click
        timer.Stop()
        timer.Enabled = False
        Me.Dispose(True)
    End Sub
    Private Sub Timer_int_change_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_int_change.Click
        Dim span As Integer = Integer.Parse(InputBox("Enter new timer interval", "Change timer interval"))
        ChangeTimerDuration(span)
    End Sub
#End Region

#Region "Prop"
    Property Device_id As String = String.Empty
#End Region

    Friend WithEvents ping As System.Net.NetworkInformation.Ping
    Friend WithEvents Timer_int_change As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents Device_close As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents timer As Timer
    Friend WithEvents btn_contextmenu As ContextMenuStrip
End Class
